﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqModel;
using CDB.LinqData;
using System.IO;
using System.Configuration;

public partial class back_architecturalWork : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    ArchitecturalWork architectWork;
    List<ArchitecturalWork> architectWorks;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["admin"] != null)
            {
                //SetInitialRowForArchitecturalWorks();
                //SetInitialRowForArchitecturalFirms();
                //bindCountry();
            }
            else
                Response.Redirect("login.aspx");

            if (Request.QueryString != null)
            {
                if (Request.QueryString["Id"] != null)
                    bindArchitecturalWork(Request.QueryString["Id"].ToString());
            }
        }
    }
    private void bindArchitecturalWork(string architectId)
    {
        architectWork = new ArchitecturalWork(_dataObject);
        architectWorks = new List<ArchitecturalWork>();
        architectWork.ArchitectId = Convert.ToInt32(architectId);
        architectWorks = architectWork.GetAllWorksByArchitectId();

        if (architectWorks != null)
        {
            grdArchitecturalWorks.DataSource = architectWorks;
            grdArchitecturalWorks.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        architectWork = new ArchitecturalWork(_dataObject);
        architectWork.ArchitectId = Convert.ToInt32(Request.QueryString["Id"].ToString());
        architectWork.Name = txtProjectName.Text;
        architectWork.DescriptionLine = txtDescription.Text;
        architectWork.City = txtCity.Text;
        architectWork.State = txtState.Text;
        architectWork.Year = txtYear.Text;
        if (uplArchitectWorkImage.HasFile)
            architectWork.ProjectImage = saveImage(uplArchitectWorkImage, architectWork.ArchitectId);
        architectWork.Create();

        bindArchitecturalWork(Request.QueryString["Id"].ToString());
        clearControls();
    }
    private void clearControls()
    {
        txtProjectName.Text = string.Empty;
        txtDescription.Text = string.Empty;
        txtCity.Text = string.Empty;
        txtState.Text = string.Empty;
        txtYear.Text = string.Empty;
    }
    private string saveImage(FileUpload fuImage, int architectId)
    {
        try
        {
            string filename = Path.GetFileName(fuImage.FileName);
            string[] fileArray = filename.Split('.');
            if (!Directory.Exists(Server.MapPath(ConfigurationManager.AppSettings["ArchitectWorkImagePath"].ToString() + architectId)))
            {
                Directory.CreateDirectory(Server.MapPath(ConfigurationManager.AppSettings["ArchitectWorkImagePath"].ToString() + architectId));
            }

            fuImage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["ArchitectWorkImagePath"].ToString() + +architectId + "/" + filename));
            return (filename);
        }
        catch (Exception ex)
        {
            return (ex.Message);
        }
    }
}